Array- In computer science it is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage. Most programming languages have a built-in array data type.
basically, it's a list. It is one variable that stores many things. Can't change the size of an array!
An array is an object that is used to store a list of values. It is made out of a contiguous block of memory that is divided into a number of "slots." Every slot stores a value, and all the values are of the same type. There are two types of arrays: indirect and direct. The difference between these two is that for direct, you have to create and initialize on the other hand indirect you have to create an array but not initialize it. The one thing that you can not do is change the size of the array.
There are 2 types of Arrays: Direct- Create and initialize Arrays
ex: int x = 5;
FilledRect <name> = new FilledRect( #, #, #, #, canvas)
FilledRect [ ] <name> = {new FilledRect ( #, #, #, #, canvas), new FilledRect ( #, #, #, #, canvas)......};
Indirect- Creating an array but not initializing it
ex: int x;
x = 5;
FilledRect <name>;
<name> = new FilledRect( #, #, #, #, canvas);
When describing an array, it looks like this:
a
b
c
d
e
0th
1st
2nd
3rd
4th
*the numbering underneath the sample array is the numbering that occurs to each slot for each element. It always starts at zero and works its way up.
When trying to call a method, print, etc. an array, you can only do it one element at a time. You need to indicate which term in the array you are going to use by stating the name ( x ) and then indicating which position in the array it is.
ex.
x [0] = a;
x [1] = b;
x [2] = c; for calling a method:
x [4].dance();
x [2].move( 1,1 );
To Create An Array: We use brackets ( [ ] ) either before or after the name of the list.
ex. int [ ] x; is the same as int x [ ];
FilledRect [ ] rectangles;
Wombat [ ] y;
In order to create an empty array with a predetermined number of slots or items:
1. Initialize the array (ex. int [ ] x = )
2. Create a new array with the number of elements you want in it ( new int [10] )
ex. int [ ] x = new int [10];
In order to create an array with values set in it:
1. Initialize the array (ex. int [ ] x = )
2. Use braces ( { } ) to enclose the set of values or elements you wish to be inside the array (ex. {1, 2, 3, 4} *just like we've used in math classes)
ex. int [ ] x = {1, 2, 3, 4, 5};
In order to create an array with elements that are not integers nor predetermined, we can use the method of initializing the array and then in the braces, call new elements.
ex. Mii [ ] myMiis = { new Mii(), new Mii(), new Mii() };
In order to create a line of code that will cause an array index out of bounds exception. ex. int[ ]a = new int [10]
You can put a[11] or a[-1]
In order to create a line of code that will create a 5-element array, with each element equal to its index. For example, index 2 equals 2.
ex. int a = {0, 1, 2, 3, 4}
In order to create a loop that will reach every element in an array called helloKitty.
ex. for (int i = 0; i < helloKitty.length; i++)
Declaring arrays: There are two ways to declare and create a one-dimensional array; someType[] a = new someType[ size];
someType[] b = {value 0, value 1, ..., value n-1}
Loops Using Arrays: When using loops with arrays, the for loop is usually best. You create the loop the same as you would for anything else, but inside the loop, use the name of the array with [ i ].
ex.
5
8
10
13
16
19
22
25
0 1 2 3 4 5 6 7
for ( int i = 1; i <= 6 ; i++)
{
x [ i ] = x [ i - 1 ] + 1;
}
&& if you trace through the loop, you will find that you change x [ 1 ] to 6, x [ 3 ] to 11, and x [ 5 ] to 17.
A shortcut to doing something to every slot in your array is to use a for loop .
for(int i=1; i < myList.length; i++){
myList[i]++;//this adds one to each slot in your array}
Practice Problems
Create codes using arrays under the following conditions:
returns true if either the first or the last slot is 6.
basically, it's a list. It is one variable that stores many things. Can't change the size of an array!
An array is an object that is used to store a list of values. It is made out of a contiguous block of memory that is divided into a number of "slots." Every slot stores a value, and all the values are of the same type. There are two types of arrays: indirect and direct. The difference between these two is that for direct, you have to create and initialize on the other hand indirect you have to create an array but not initialize it. The one thing that you can not do is change the size of the array.
There are 2 types of Arrays:
Direct- Create and initialize Arrays
ex: int x = 5;
FilledRect <name> = new FilledRect( #, #, #, #, canvas)
FilledRect [ ] <name> = {new FilledRect ( #, #, #, #, canvas), new FilledRect ( #, #, #, #, canvas)......};
Indirect- Creating an array but not initializing it
ex: int x;
x = 5;
FilledRect <name>;
<name> = new FilledRect( #, #, #, #, canvas);
When describing an array, it looks like this:
When trying to call a method, print, etc. an array, you can only do it one element at a time. You need to indicate which term in the array you are going to use by stating the name ( x ) and then indicating which position in the array it is.
ex.
x [0] = a;
x [1] = b;
x [2] = c;
for calling a method:
x [4].dance();
x [2].move( 1,1 );
To Create An Array:
We use brackets ( [ ] ) either before or after the name of the list.
ex. int [ ] x; is the same as int x [ ];
FilledRect [ ] rectangles;
Wombat [ ] y;
In order to create an empty array with a predetermined number of slots or items:
1. Initialize the array (ex. int [ ] x = )
2. Create a new array with the number of elements you want in it ( new int [10] )
ex. int [ ] x = new int [10];
In order to create an array with values set in it:
1. Initialize the array (ex. int [ ] x = )
2. Use braces ( { } ) to enclose the set of values or elements you wish to be inside the array (ex. {1, 2, 3, 4} *just like we've used in math classes)
ex. int [ ] x = {1, 2, 3, 4, 5};
In order to create an array with elements that are not integers nor predetermined, we can use the method of initializing the array and then in the braces, call new elements.
ex. Mii [ ] myMiis = { new Mii(), new Mii(), new Mii() };
In order to create a line of code that will cause an array index out of bounds exception.
ex. int[ ]a = new int [10]
You can put a[11] or a[-1]
In order to create a line of code that will create a 5-element array, with each element equal to its index. For example, index 2 equals 2.
ex. int a = {0, 1, 2, 3, 4}
In order to create a loop that will reach every element in an array called helloKitty.
ex. for (int i = 0; i < helloKitty.length; i++)
Declaring arrays:
There are two ways to declare and create a one-dimensional array;
someType[] a = new someType[ size];
someType[] b = {value 0, value 1, ..., value n-1}
Loops Using Arrays:
When using loops with arrays, the for loop is usually best. You create the loop the same as you would for anything else, but inside the loop, use the name of the array with [ i ].
ex.
for ( int i = 1; i <= 6 ; i++)
{
x [ i ] = x [ i - 1 ] + 1;
}
&& if you trace through the loop, you will find that you change x [ 1 ] to 6, x [ 3 ] to 11, and x [ 5 ] to 17.
A shortcut to doing something to every slot in your array is to use a for loop .
Practice Problems
Create codes using arrays under the following conditions:
returns true if either the first or the last slot is 6.
returns true if the first and the last slots are equal.
creates and returns an array with the first 3 digits of pi.